package com.cy.cy2025.activity

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.cy.cy2025.R
import com.cy.cy2025.databinding.ActivityPictureBinding
import com.cy.cy2025.util.LogUtil

class PictureActivity : AppCompatActivity() {

    val binding by lazy {
        ActivityPictureBinding.inflate(layoutInflater)
    }

    val vm by lazy {
        ViewModelProvider(this)[PictureVm::class.java]
    }

    var launcher1: ActivityResultLauncher<Intent>? = null
    var launcher2: ActivityResultLauncher<PickVisualMediaRequest>? = null
    var launcher3: ActivityResultLauncher<PickVisualMediaRequest>? = null


    var adapter = object : BaseQuickAdapter<Uri, QuickViewHolder>() {
        override fun onCreateViewHolder(
            context: Context,
            parent: ViewGroup,
            viewType: Int
        ): QuickViewHolder {
            return QuickViewHolder(R.layout.item_picture, parent)
        }

        override fun onBindViewHolder(
            holder: QuickViewHolder,
            position: Int,
            item: Uri?
        ) {
            Glide.with(context).load(item).into(holder.getView<ImageView>(R.id.imageView))
        }

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(binding.root)


        binding.rvImages.adapter = adapter

        vm.data.observe(this) {
            adapter.submitList(it)
        }

        launcher1 = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { it ->
                if (it.resultCode == RESULT_OK) {
                    val clipData = it.data?.clipData
                    val size = clipData?.itemCount ?: 0
                    val list = mutableListOf<Uri>()
                    for (i in 0 until size) {
                        val uri = clipData?.getItemAt(i)?.uri
                        if (uri != null){
                            list.add(uri)
                        }
                    }
                    vm.updateData(list)
                }

            }

        //通过Intent获取图片
        binding.btnIntent.setOnClickListener {
            val intent = Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
            )
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
            launcher1?.launch(intent)

        }



        //官方activity.ktx获取单张
        launcher2 = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
            uri?.let {
                val list = mutableListOf<Uri>(it)
                vm.updateData(list)
            }
        }

        //通过官方半页弹窗获取单张
        binding.btnOne.setOnClickListener {
            launcher2?.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
        }


        //官方activity.ktx获取多张
        launcher3 = registerForActivityResult(ActivityResultContracts.PickMultipleVisualMedia(9)) { uris ->
            uris?.let {
                vm.updateData(uris)
            }
        }

        //通过官方半页弹窗获取单张
        binding.btnMore.setOnClickListener {
            launcher3?.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
        }
    }
}

class PictureVm : ViewModel() {

    private val _data = MutableLiveData<MutableList<Uri>>()
    val data: LiveData<MutableList<Uri>> = _data

    fun updateData(uris: List<Uri>) {
        var list = _data.value ?: mutableListOf()
        list.clear()
        list.addAll(uris)
        _data.value = list
    }
}